//
//  DHUserManagerViewController.m
//  Dresshere
//
//  Created by braspring on 2017/12/27.
//  Copyright © 2017年 张朋. All rights reserved.
//

#import "DHUserManagerViewController.h"
#import "HDUserManagerHeadCell.h"
#import "DHUserManagerNormalCell.h"
#import "DHS2_BuyMemberController.h"
#import "DHLoginGenderViewController.h"
#import "DHLoginStyleViewController.h"
#import "DHLoginInfoViewController.h"
#import "DHMembCheckController.h"
#import "DHUserAddressesController.h"
#import "Address.h"

@interface DHUserManagerViewController ()<UITableViewDelegate, UITableViewDataSource>

@property (weak, nonatomic) IBOutlet UITableView *tableView;

@property (strong, nonatomic) NSArray *addressesArr;

@end

@implementation DHUserManagerViewController

- (void)viewWillAppear:(BOOL)animated{
    [super viewWillAppear:animated];
    [self.tableView reloadData];
}

- (NSArray *)addressesArr{
    if (!_addressesArr) {
        _addressesArr = [NSArray array];
    }
    return _addressesArr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    self.titleLabel.text = @"会员";
    
    [self.tableView registerNib:[UINib nibWithNibName:@"HDUserManagerHeadCell" bundle:nil] forCellReuseIdentifier:kHDUserManagerHeadCell];
    [self.tableView registerNib:[UINib nibWithNibName:@"DHUserManagerNormalCell" bundle:nil] forCellReuseIdentifier:kDHUserManagerNormalCell];
    [self.tableView setSeparatorStyle:UITableViewCellSeparatorStyleNone];
    
    [self loadUserAddresses];
    
    addNObserver(@selector(liangti:), @"liangti");
}

- (void)dealloc{
    removeNObserver(@"liangti");
}

- (void)liangti:(NSNotification *)not{
    if (not){
        NSDictionary *dic = not.object;
        Address *address = [dic objectForKey:@"address"];
        
        NSDictionary *params = @{@"name":address.name,
                                 @"province":address.province,
                                 @"city":address.city,
                                 @"district":address.district,
                                 @"address":address.address,
                                 @"accurate":address.accurate,
                                 @"phone":address.phone
                                 };
        [[DHNetAPIClient shareDHNetAPIClient] request_user_measureWithParams:params andBlock:^(id data, NSError *error) {
            [NSObject showHudTipStr:@"预约成功"];
        }];
    }
}

- (void)loadUserAddresses{
    [[DHNetAPIClient shareDHNetAPIClient] request_user_addressAndBlock:^(id data, NSError *error) {
        if (data) {
            self.addressesArr = [data objectForKey:@"addresses"];
            [self.tableView reloadData];
        }
    }];
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

#pragma mark - UITableViewDelegate and UITableViewDataSource
- (NSInteger)numberOfSectionsInTableView:(UITableView *)tableView{
    return 3;
}

- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)section{
    if (section == 0 || section == 2) {
        return 1;
    }else if (section == 1){
        return 1;
    }else if (section == 3){
        return 2;
    }
    return 0;
}

- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        return 170;
    }else{
        return 44;
    }
}

- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.section == 0) {
        HDUserManagerHeadCell *cell = [tableView dequeueReusableCellWithIdentifier:kHDUserManagerHeadCell forIndexPath:indexPath];
        [cell updateCell];
        [cell setSelectionStyle:UITableViewCellSelectionStyleNone];
        return cell;
    }else{
        DHUserManagerNormalCell *cell = [tableView dequeueReusableCellWithIdentifier:kDHUserManagerNormalCell forIndexPath:indexPath];
        cell.lineView.hidden = NO;
        [cell.hintButton setImage:[UIImage imageNamed:@""] forState:UIControlStateNormal];
        if (indexPath.section == 1) {
            if (indexPath.row == 0) {
                [cell.hintButton setImage:[UIImage imageNamed:@"home_crown"] forState:UIControlStateNormal];
                [cell.hintButton setTitleColor:[UIColor colorWithHexString:@"#9E8226"] forState:UIControlStateNormal];
                if ([GlobalManager shareGlobalManager].vip.vip) {
                    NSDate *date = [NSDate dateWithServerISOString:[[GlobalManager shareGlobalManager].vip.info objectForKey:@"expire"] format:@"yyyy-MM-dd'T'HH:mm:ss.SSSZ"];
                    cell.titleLabel.text = [NSString stringWithFormat:@"%@到期",[NSDate stringFromDate:date withFormat:@"yyyy/MM/dd"]];
                    [cell.hintButton setTitle:[NSString stringWithFormat:@" %@",[[GlobalManager shareGlobalManager].vip.info objectForKey:@"name"]] forState:UIControlStateNormal];
                }else{
                    cell.titleLabel.text = @"";
                    [cell.hintButton setTitle:@"  未加入" forState:UIControlStateNormal];
                }
            }
//            else if (indexPath.row == 1){
//                cell.titleLabel.text = @"实名认证";
//                [cell.hintButton setTitle:@"未完成" forState:UIControlStateNormal];
//                cell.lineView.hidden = YES;
//            }
        }else if (indexPath.section == 3){
            if (indexPath.row == 0) {
                cell.titleLabel.text = @"尺码";
                [cell.hintButton setTitle:[NSString stringWithFormat:@"%ld/%ld/%ld",(long)[GlobalManager shareGlobalManager].user.height,[GlobalManager shareGlobalManager].user.weight,[GlobalManager shareGlobalManager].user.age] forState:UIControlStateNormal];
            }else if (indexPath.row == 1){
                cell.titleLabel.text = @"喜好风格";
                [cell.hintButton setTitle:[NSString stringWithFormat:@"%lu种",(unsigned long)[[GlobalManager shareGlobalManager].user.styles count]] forState:UIControlStateNormal];
                cell.lineView.hidden = YES;
            }
        }else if (indexPath.section == 2){
            cell.titleLabel.text = @"常用地址";
            [cell.hintButton setTitle:[NSString stringWithFormat:@"%lu个",(unsigned long)self.addressesArr.count] forState:UIControlStateNormal];
            cell.lineView.hidden = YES;
        }
        return cell;
    }
}

- (void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath{
    [tableView deselectRowAtIndexPath:indexPath animated:YES];
    if (indexPath.section == 1 && indexPath.row == 0) {
        if (![GlobalManager shareGlobalManager].vip.vip) {
            DHS2_BuyMemberController *buyController = [[DHS2_BuyMemberController alloc] init];
            [self.navigationController pushViewController:buyController animated:YES];
        }else{
            DHMembCheckController *vc = [[DHMembCheckController alloc] init];
            [self.navigationController pushViewController:vc animated:YES];
        }
    }else if (indexPath.section == 3 && indexPath.row == 2) {
        DHLoginGenderViewController *genderController = [[DHLoginGenderViewController alloc] init];
        genderController.userManagerPresent = YES;
        [self presentViewController:genderController animated:YES completion:nil];
    }else if (indexPath.section == 3 && indexPath.row == 1) {
        DHLoginStyleViewController *styleController = [[DHLoginStyleViewController alloc] init];
        styleController.userManagerPresent = YES;
        [self presentViewController:styleController animated:YES completion:nil];
    }else if (indexPath.section == 3 && indexPath.row == 0) {
        DHLoginInfoViewController *styleController = [[DHLoginInfoViewController alloc] init];
        styleController.userManagerPresent = YES;
        [self presentViewController:styleController animated:YES completion:nil];
    }else if (indexPath.section == 2){
        DHUserAddressesController *vc = [[DHUserAddressesController alloc] init];
        vc.arr = self.addressesArr;
        [self.navigationController pushViewController:vc animated:YES];
    }
}

//section头部间距
- (CGFloat)tableView:(UITableView *)tableView heightForHeaderInSection:(NSInteger)section{
    if (section == 0 || section == 4) {
        return 1;
    }else{
        return 30;
    }
}
//section头部视图
- (UIView *)tableView:(UITableView *)tableView viewForHeaderInSection:(NSInteger)section{
    if (section == 0) {
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 1)];
        view.backgroundColor = [UIColor clearColor];
        return view ;
    }else{
        UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, kScreenWidth, 30)];
        view.backgroundColor = [UIColor whiteColor];
        UILabel *label = [[UILabel alloc] initWithFrame:CGRectMake(15, 0, view.width, 30)];
        label.backgroundColor = [UIColor clearColor];
        label.font = [UIFont boldSystemFontOfSize:12];
        label.textColor = [UIColor colorWithHexString:@"#7B7B7B"];
        if (section == 1) {
            label.text = @"付费会员";
        }else if (section == 3){
            label.text = @"个人信息";
        }else if (section == 2){
            label.text = @"地址管理";
        }
        UIView *lineView = [[UIView alloc] initWithFrame:CGRectMake(15, view.height - 1, view.width, 1)];
        lineView.backgroundColor = [UIColor colorWithHexString:@"#F6F6F6"];
        [view addSubview:label];
        [view addSubview:lineView];
        return view ;
    }
}
//section底部间距
- (CGFloat)tableView:(UITableView *)tableView heightForFooterInSection:(NSInteger)section{
    return 13;
}
//section底部视图
- (UIView *)tableView:(UITableView *)tableView viewForFooterInSection:(NSInteger)section{
    UIView *view=[[UIView alloc] initWithFrame:CGRectMake(0, 0, 320, 13)];
    view.backgroundColor = [UIColor clearColor];
    return view;
}

@end
